const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '',
    isable:true,
    isdoublable:false,
    name:'',
    cardNo:'',
    mobile:'',
    code:'',
    xyht:false,
    status:'0',
    title:'校长',
    persole:'1',
    codeFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var id = options.id;
    this.setData({
      persole:id
    })
    if(id == 1){
      this.setData({
        title:'校长'
      })
    }
    if(id == 2){
      this.setData({
        title:'财务'
      })
    }
    if(id == 3){
      this.setData({
        title:'招生'
      })
    }
    if(id == 4){
      this.setData({
        title:'教练'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getCode() {
    var that = this
    //console.log()
    if (this.data.mobile == '') {
      wx.showModal({
        title: '提示',
        content: '请填写联系电话！',
        showCancel: false
      })
      return;
    }
    if (this.data.mobile.length < 11) {
      wx.showModal({
        title: '提示',
        content: '请检查手机号码是否正确！',
        showCancel: false
      })
      return;
    }
    if (this.data.xyht != true) {
      wx.showModal({
        title: '提示',
        content: '请勾选同意协议！',
        showCancel: false
      })
      return;
    }
    util.request(api.Verificationinformation, {
      name: this.data.name,
      cardNo: this.data.cardNo,
      mobile: this.data.mobile
      //id: this.data.persole
    }).then(function (res) {
      console.log(JSON.stringify(res))
      // console.log(that.data.persole)
      if (res.errno == '401') {
        wx.showModal({
          title: '提示',
          content: res.errmsg,
          showCancel: false
        })
        return;
      }
      if (res.errno == '404') {
        wx.showModal({
          title: '提示',
          content: res.errmsg,
          showCancel: false
        })
        return;
      }
      if (res.errno == '0') {
        //进行发送验证码
        setTimeout(function () {
          that.openNewCode()
        }, 30 * 1000)
        that.setData({
          codeFlag: true
        })
        util.request(api.getCodeUrl, {
          mobile: that.data.mobile
        }).then(function (res) {
          wx.showToast({
            title: '短信验证码已经发送成功！',
            icon: 'none'
          })
        })
      }
    })
  },
  openNewCode(){
    this.setData({
      codeFlag: false
    })
  },
  bindUser() {
    var that = this;
    if(!this.data.xyht){
      wx.showModal({
        title: '提示',
        content: '请先认真阅读并同意协议！',
        showCancel: false
      })
      return;
    }
    if(this.data.name == ''){
      wx.showModal({
        title: '提示',
        content: '请填写姓名！',
        showCancel: false
      })
      return;
    }
    if(this.data.cardNo == ''){
      wx.showModal({
        title: '提示',
        content: '请填写证件号码！',
        showCancel: false
      })
      return;
    }
    if(this.data.mobile == ''){
      wx.showModal({
        title: '提示',
        content: '请填写联系电话！',
        showCancel: false
      })
      return;
    }
    if(this.data.code == ''){
      wx.showModal({
        title: '提示',
        content: '请输入验证码！',
        showCancel: false
      })
      return;
    }
    var userid = wx.getStorageSync("userid");
    util.request(api.bindManagerUrl, {
      id:userid,
      name:this.data.name,
      cardNo:this.data.cardNo,
      mobile:this.data.mobile,
      persole:this.data.persole,
      code:this.data.code
    }).then(function (res) {
      console.log(JSON.stringify(res));
      if(res.errno == '404'){
        wx.showModal({
          title: '提示',
          content: res.errmsg,
          showCancel: false
        })
        return;
      }

      if(res.errno == '0'){
        wx.showModal({
          title: '提示',
          content: '绑定成功！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.switchTab({
                url: '../total/index'
              })
            }
          }
        })
      }


    })



  },
  checkboxChange:function(e){
    if(typeof(e.detail.value[0]) != 'undefined'){
      this.setData({
        xyht:true
      })
    }else{
      this.setData({
        xyht:false
      })
    }
  },
  bindname: function (e) {
    this.setData({
      name: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindcardNo: function (e) {
    this.setData({
      cardNo: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindnmobile: function (e) {
    this.setData({
      mobile: e.detail.value.replace(/\s+/g, '')
    })
  },
  bindCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  }
})